import { Component } from '@angular/core';
import { Storage } from '@ionic/storage';
import { IonicPage, NavController, ViewController, AlertController, NavParams, Events } from 'ionic-angular';
import { TranslateService } from '@ngx-translate/core';

import { GlobalData } from '../../providers/GlobalData';
import { LoginPage } from '../login/login';
import { OfflineService } from "../../providers/services.offline";
import { HelperService } from "../../providers/services.helper";

/**
 * Generated class for the SettingsModePage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-settings-mode',
  templateUrl: 'settings-mode.html',
})
export class SettingsModePage {

  constructor(public navCtrl: NavController,
    public viewCtrl: ViewController,
    private global: GlobalData,
    private storage: Storage,
    private helperService: HelperService,
    public alertCtrl: AlertController,
    public translate: TranslateService,
    public offline: OfflineService,
    private events: Events,
    public navParams: NavParams) {

  }

  selectedEnvir(type) {
    this.global.curEnvir = type;
    console.log('selected envir:', type);

    this.storage.get('settings').then(setting => {

      if (this.global.curEnvir == 'SANDBOX_MODE') {
        setting.sandbox_mode = true;
      } else {
        setting.sandbox_mode = false;
      }

      this.storage.set('settings', setting);
    });
    this.storage.get('user').then((user) => {
      user.isLogin = false;
      this.storage.set('user', user);
    });
    this.dismiss('tologin');

  }

  selectedMode(type) {
    this.global.curMode = type;

    this.storage.get('settings').then(setting => {

      if (this.global.curMode == 'ONLINE_MODE') {
        setting.online_mode = true;
      } else {
        setting.online_mode = false;
      }

      this.storage.set('settings', setting);
    });
  }

  downloadOfflineData(remindStr) {
    console.log('downloadOfflineData');
    let alert = this.alertCtrl.create({
      title: this.translate.instant('REMIND'),
      message: this.translate.instant(remindStr),
      buttons: [
        {
          text: this.translate.instant('CANCEL'),
          role: 'cancel',
          handler: () => {
            console.log('Cancel clicked');
          }
        },
        {
          text: this.translate.instant('DOWNLOAD'),
          handler: () => {
            console.log('Confirm download offline data');
            this.storage.get('settings').then((settings) => {
              this.offline.getOfflineData({
                token: settings.token,
                locale: settings.locale,
                category: 'POS',
                type: 'SQLite'
              }).then(() => {
                console.log('getOfflineData');
                this.helperService.showToast(this.translate.instant('DOWNLOAD_SUCCESS'));
                this.dismiss(null);
                // this.events.publish('modaloperation:dismiss', Date.now());

              }, (err) => {
                this.helperService.showToast(err);
              });
            });
          }
        }
      ]
    });
    alert.present();
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad SettingsModePage');
  }

  // dismiss table list modal
  dismiss(type) {
    this.viewCtrl.dismiss(type);
  }
}
